package com.chaoxing;

import com.chaoxing.service.ChaoXingService;
import com.chaoxing.entity.Course;
import com.chaoxing.entity.Activity;
import com.chaoxing.entity.SignResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;

import java.util.List;
import java.util.Scanner;

@Slf4j
@SpringBootApplication
@Profile("test")
public class DetailedTestRunner implements CommandLineRunner {
    
    @Autowired
    private ChaoXingService chaoXingService;
    
    public static void main(String[] args) {
        System.setProperty("spring.profiles.active", "test");
        SpringApplication.run(DetailedTestRunner.class, args);
    }
    
    @Override
    public void run(String... args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        
        log.info("=== 学习通签到系统详细测试 ===");
        
        // 1. 测试登录
        log.info("\n[步骤1] 测试登录功能...");
        log.info("正在使用配置的账号密码登录...");
        
        boolean loginSuccess = chaoXingService.login();
        if (loginSuccess) {
            log.info("✓ 登录成功！");
        } else {
            log.error("✗ 登录失败，请检查：");
            log.error("  1. 账号密码是否正确");
            log.error("  2. 网络是否正常");
            log.error("  3. 是否需要验证码");
            return;
        }
        
        // 2. 测试获取课程
        log.info("\n[步骤2] 获取课程列表...");
        List<Course> courses = chaoXingService.getCourses();
        
        if (courses.isEmpty()) {
            log.warn("未获取到任何课程，可能原因：");
            log.warn("  1. 账号没有加入任何课程");
            log.warn("  2. Cookie已过期");
            return;
        }
        
        log.info("✓ 获取到 {} 门课程：", courses.size());
        for (int i = 0; i < courses.size(); i++) {
            Course course = courses.get(i);
            log.info("  {}. {} (教师: {}, ID: {})", 
                i + 1, 
                course.getCourseName(), 
                course.getTeacherName(),
                course.getCourseId());
        }
        
        // 3. 检查签到活动
        log.info("\n[步骤3] 检查签到活动...");
        log.info("正在遍历所有课程查找活动...");
        
        boolean foundActivity = false;
        for (Course course : courses) {
            log.debug("检查课程: {}", course.getCourseName());
            List<Activity> activities = chaoXingService.getActivities(course);
            
            if (!activities.isEmpty()) {
                foundActivity = true;
                log.info("\n✓ 课程 [{}] 发现 {} 个待签到活动：", course.getCourseName(), activities.size());
                
                for (Activity activity : activities) {
                    log.info("  - 活动名称: {}", activity.getName());
                    log.info("    活动ID: {}", activity.getActiveId());
                    log.info("    签到类型: {} (otherId={})", activity.getType().getDescription(), activity.getOtherId());
                    log.info("    活动状态: {}", activity.isActive() ? "进行中" : "已结束");
                    
                    // 询问是否尝试签到
                    log.info("\n是否尝试签到此活动？(y/n)");
                    String answer = scanner.nextLine();
                    
                    if ("y".equalsIgnoreCase(answer)) {
                        log.info("正在尝试签到...");
                        SignResult result = chaoXingService.doSign(activity, course);
                        
                        if (result.isSuccess()) {
                            log.info("✓ 签到成功！");
                        } else {
                            log.error("✗ 签到失败: {}", result.getMessage());
                            
                            // 对于特殊类型的签到给出提示
                            switch (activity.getType()) {
                                case QRCODE:
                                    log.info("提示: 二维码签到需要扫描二维码获取enc参数");
                                    break;
                                case PHOTO:
                                    log.info("提示: 拍照签到需要上传照片");
                                    break;
                                case LOCATION:
                                    log.info("提示: 位置签到需要在配置文件中设置正确的经纬度");
                                    break;
                                case GESTURE:
                                    log.info("提示: 手势签到需要知道正确的手势");
                                    break;
                                case CODE:
                                    log.info("提示: 签到码签到需要输入正确的签到码");
                                    break;
                            }
                        }
                    }
                }
            }
        }
        
        if (!foundActivity) {
            log.info("\n当前没有待签到的活动");
            log.info("提示：");
            log.info("  1. 可以让老师发布一个签到活动再测试");
            log.info("  2. 签到活动开始2小时后会被忽略");
        }
        
        // 4. 测试自动签到调度器
        log.info("\n[步骤4] 自动签到说明");
        log.info("启动正式程序后，系统会每5分钟自动检查一次签到活动");
        log.info("发现活动后会自动签到并发送通知");
        
        log.info("\n=== 测试完成 ===");
        log.info("如需启动自动签到，请运行: mvn spring-boot:run");
        
        scanner.close();
    }
}